/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.saber.certificacao.ejbs.jms;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.mail.MessagingException;
import javax.naming.NamingException;
import br.com.saber.certificacao.ejbs.jms.jmsEmailUtilitarios.interfaces.EmailSendInterface;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
/**
 *
 * @author gbvbahia
 */
@MessageDriven(mappedName = "jms/EmailSaber", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "MessageFormat = 'saber_msg'")
})
public class EmailsJms implements MessageListener {

    private String characterCoding = "iso-8859-1";  
    
    @Resource(name="emailSenha")
    private String senhaEmail;
      
    public EmailsJms() {
    }

    @Override
    public void onMessage(Message message) {
        EmailSendInterface email = null;
        ObjectMessage objMsg = (ObjectMessage) message;
        try {
            email = (EmailSendInterface) objMsg.getObject();
            sendMail(email.getEmail(), email.getSubject(), email.getBody());
        } catch (JMSException ex) {
            Logger.getLogger(EmailsJms.class.getName()).log(Level.WARNING, "onMessage 1º catch", ex);
        } catch (MessagingException msgEx) {
            Logger.getLogger(EmailsJms.class.getName()).log(Level.WARNING, "onMessage 2º catch", msgEx);
        } catch (NamingException nEx) {
            Logger.getLogger(EmailsJms.class.getName()).log(Level.WARNING, "onMessage 3º catch", nEx);
        }
    }

    private void sendMail(String e_mail, String subject, String body) throws NamingException, MessagingException {
       try{
        HtmlEmail email = new HtmlEmail();
        email.setDebug(false);
        email.setHostName("gmail-smtp.l.google.com");
        email.setCharset(characterCoding);
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator("gbvbahia01@gmail.com", senhaEmail));
        email.setTLS(true);
        email.setHtmlMsg(body);
        email.addTo(e_mail);
        email.setFrom("gbvbahia01@gmail.com","Saber Certificação");
        email.setSubject(subject);
        email.setSocketConnectionTimeout(1000 * 60 * 3);
        email.setSocketTimeout(1000 * 60 * 3);
        email.setSSL(true);
        email.setSslSmtpPort("465");
        email.send();
        Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Email enviado com sucesso! ==> '{'{0}'}'", e_mail);
       }catch(EmailException e){
           throw new MessagingException(e_mail, e);
       }
    }
}
